package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.ResponseEntity;
import com.sofwin.dto.UserDto;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.DeptService;
import com.sofwin.service.UserService;
import com.sofwin.zj.Log;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @packageName: com.sofwin.controller
 * @user: andyliu
 * @date: 2022/12/29 10:03
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;

    /**
     * 跳转到列表页
     * @return
     */
    @GetMapping()
    public String toList(SysUser user, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue = Constants.PAGE_SIZE) Integer pageSize, ModelMap map){
        PageInfo<UserDto> pageInfo = userService.queryUsers(pageNumber, pageSize, user);
        map.put("page",pageInfo);
        // web-inf/user/list.jsp
        return "user/list";
    }

    @GetMapping("toPwd")
    public String toPwd(){
        return "user/pwd";
    }

    /**
     * 为列表页提供数据
     * @param user
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @PostMapping("/data")
    @ResponseBody
    @Log(module = "用户分页查询")
    public ResponseEntity data(SysUser user, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue = Constants.PAGE_SIZE) Integer pageSize){
        PageInfo<UserDto> pageInfo = userService.queryUsers(pageNumber, pageSize, user);
        return ResponseEntity.success(pageInfo);
    }

    /**
     * 跳转到新增或编辑页
     * @return
     */
    @GetMapping("toAdd")
    public String toAdd(Integer id, Model model){
        if(id!=null){
            SysUser user = userService.queryById(id);
            model.addAttribute("user",user);

        }
        model.addAttribute("depts",deptService.queryAll());
        return "user/edit";
    }

    /**
     * 为编辑页提供数据
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @ResponseBody
    @Log(module = "根据Id查询")
    public ResponseEntity info(@PathVariable Integer id){
        SysUser sysUser = userService.queryById(id);
        return ResponseEntity.success(sysUser);
    }

    /**
     * 单个删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @Log(module = "用户删除")
    public ResponseEntity delete(@PathVariable Integer id){
        int i=1/0;
        boolean status =  userService.removeById(id);
        if(status){
            return ResponseEntity.success(status);
        }
        return ResponseEntity.error(status);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    @Log(module = "用户批量删除")
    public ResponseEntity deletes(Integer[] ids){
        boolean status =  userService.removeByIds(ids);
        if(status){
            return ResponseEntity.success(status);
        }
        return ResponseEntity.error(status);
    }

    /**
     * 新增或编辑请求
     * @param user
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    @Log(module = "用户保存")
    public ResponseEntity save(SysUser user){
        boolean status =  userService.saveOrUpdateUser(user);
        if(status){
            return ResponseEntity.success(status);
        }
        return ResponseEntity.error(status);
    }
    @GetMapping("checkPwd")
    @ResponseBody
    public boolean checkPwd(String pwd, HttpSession session){
        SysUser user = (SysUser)session.getAttribute(Constants.CURRENT_USER);
        String pwdHashed = new SimpleHash("MD5",pwd,user.getBz(),10).toString();
        SysUser sysUser = userService.queryUserByLoginNameAndPwd(user.getLoginName(), pwdHashed);
        if(sysUser==null){
            return  false;
        }
        return true;
    }

}
